/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.worksheet.service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.act.service.ActTaskService;
import com.thinkgem.jeesite.modules.act.utils.ActUtils;
import com.thinkgem.jeesite.modules.duty.entity.ZlmtMaintenanceRecord;
import com.thinkgem.jeesite.modules.duty.service.ZlmtMaintenanceRecordService;
import com.thinkgem.jeesite.modules.inventory.entity.HostInventory;
import com.thinkgem.jeesite.modules.inventory.service.HostInventoryService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.worksheet.entity.ZlmtResourceWorksheet;
import com.thinkgem.jeesite.modules.worksheet.entity.ZlmtWorksheetFiles;
import com.thinkgem.jeesite.modules.worksheet.entity.ZlmtWorksheetNote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.worksheet.entity.ZlmtWorksheet;
import com.thinkgem.jeesite.modules.worksheet.dao.ZlmtWorksheetDao;

/**
 * 工单管理Service
 * @author johnKee
 * @version 2016-07-17
 */
@Service
@Transactional(readOnly = true)
public class ZlmtWorksheetService extends CrudService<ZlmtWorksheetDao, ZlmtWorksheet> {

	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private ZlmtWorksheetNoteService zlmtWorksheetNoteService;
	@Autowired
	private ZlmtWorksheetFilesService zlmtWorksheetFilesService;
	@Autowired
	private ZlmtResourceWorksheetService zlmtResourceWorksheetService;
	@Autowired
	private HostInventoryService hostInventoryService;
	@Autowired
	private ZlmtMaintenanceRecordService zlmtMaintenanceRecordService;

	public ZlmtWorksheet get(String id) {
		return super.get(id);
	}
	
	public List<ZlmtWorksheet> findList(ZlmtWorksheet zlmtWorksheet) {
		return super.findList(zlmtWorksheet);
	}
	
	public Page<ZlmtWorksheet> findPage(Page<ZlmtWorksheet> page, ZlmtWorksheet zlmtWorksheet) {
		return super.findPage(page, zlmtWorksheet);
	}

	private Map<String, Object> setWorksheetDeailer(ZlmtWorksheet zlmtWorksheet) {
		Map<String, Object> vars = Maps.newHashMap();
		String procDefId = zlmtWorksheet.getAct().getProcDefId();
		if (procDefId.contains(ActUtils.PD_BIZ_APPLY_RESOURCE[0])) {
			vars.put("bizLeader", "sd_admin");
			vars.put("itLeader", "sd_admin");
			vars.put("doEmp", "sd_admin");
		} else if (procDefId.contains(ActUtils.PD_HANDING_EXCEPTION[0])) {
			vars.put("bizLeader", "sd_admin");
			vars.put("itLeader", "sd_admin");
			vars.put("doEmps", Arrays.asList("sd_admin", "jn_admin"));
		}
		return vars;
	}

	@Transactional(readOnly = false)
	public void save(ZlmtWorksheet zlmtWorksheet) {
		String procDefId = zlmtWorksheet.getAct().getProcDefId();
		// 设置流程处理人员
		Map<String, Object> vars = setWorksheetDeailer(zlmtWorksheet);
		// 申请发起
		if (StringUtils.isBlank(zlmtWorksheet.getId())){
			zlmtWorksheet.preInsert();
			dao.insert(zlmtWorksheet);
			if (zlmtWorksheet.getZlmtWorksheetFile().getFileUrl() != null) { //保存上传附件信息
				String fileName;
				String[] filesUrl = zlmtWorksheet.getZlmtWorksheetFile().getFileUrl().split("\\|");
				for (String url : filesUrl) {
					if (StringUtils.isNotBlank(url)) {
						String[] names = url.split("/");
						fileName = names[names.length - 1];
						ZlmtWorksheetFiles files = new ZlmtWorksheetFiles();
						files.setFileUrl(url);
						files.setWorksheetId(zlmtWorksheet.getId());
						files.setFileName(fileName);
						zlmtWorksheetFilesService.save(files);
					}
				}
			}
			if (zlmtWorksheet.getZlmtResourceWorksheet().getHostid() != null) { //保存资源派工信息
				HostInventory hostInventory = hostInventoryService.get(zlmtWorksheet.getZlmtResourceWorksheet().getHostid());
				ZlmtResourceWorksheet resource = new ZlmtResourceWorksheet();
				resource.setHostid(zlmtWorksheet.getZlmtResourceWorksheet().getHostid());
				resource.setHostName(hostInventory.getName());
				resource.setAlias(hostInventory.getAlias());
				resource.setWorksheetId(zlmtWorksheet.getId());
				zlmtResourceWorksheetService.save(resource);
			}
			// 启动流程
			if (procDefId.contains(ActUtils.PD_BIZ_APPLY_RESOURCE[0])) {
				actTaskService.startProcess(ActUtils.PD_BIZ_APPLY_RESOURCE[0], ActUtils.PD_BIZ_APPLY_RESOURCE[1], zlmtWorksheet.getId(), zlmtWorksheet.getWorksheetTitle(), vars);
			} else if (procDefId.contains(ActUtils.PD_HANDING_EXCEPTION[0])) {
				actTaskService.startProcess(ActUtils.PD_HANDING_EXCEPTION[0], ActUtils.PD_HANDING_EXCEPTION[1], zlmtWorksheet.getId(), zlmtWorksheet.getWorksheetTitle(), vars);
			}
		} else { // 重新编辑申请
			zlmtWorksheet.preUpdate();
			dao.update(zlmtWorksheet);

			zlmtWorksheet.getAct().setComment(("yes".equals(zlmtWorksheet.getAct().getFlag())?"[重申] ":"[销毁] ")+zlmtWorksheet.getAct().getComment());

			// 完成流程任务
			vars.put("pass", "yes".equals(zlmtWorksheet.getAct().getFlag())? "1" : "0");
			actTaskService.complete(zlmtWorksheet.getAct().getTaskId(), zlmtWorksheet.getAct().getProcInsId(), zlmtWorksheet.getAct().getComment(), zlmtWorksheet.getWorksheetTitle(), vars);
		}
	}
	/**
	 * 审核审批保存
	 * @param zlmtWorksheet
	 */
	@Transactional(readOnly = false)
	public void auditSave(ZlmtWorksheet zlmtWorksheet) {
		String procDefId = zlmtWorksheet.getAct().getProcDefId();
		// 设置流程处理人员
		Map<String, Object> vars = setWorksheetDeailer(zlmtWorksheet);
		// 设置意见
		zlmtWorksheet.getAct().setComment(("yes".equals(zlmtWorksheet.getAct().getFlag())?"[同意] ":"[驳回] ")+zlmtWorksheet.getAct().getComment());

		zlmtWorksheet.preUpdate();

		// 对不同环节的业务逻辑进行操作
		String taskDefKey = zlmtWorksheet.getAct().getTaskDefKey();

		// 审核环节
		if (zlmtWorksheet.getRealBeginTime() == null) {
			zlmtWorksheet.setRealBeginTime(new Date());
			dao.update(zlmtWorksheet);
		}
		if (taskDefKey.contains("audit") || taskDefKey.contains("Audit")){
			ZlmtWorksheetNote note = new ZlmtWorksheetNote();
			note.setWorksheetId(zlmtWorksheet.getId());
			note.setNoteType("NOTE_AUDIT");
			note.setNoteContent(zlmtWorksheet.getAct().getComment());
			zlmtWorksheetNoteService.save(note);
		} else if ("dojob".equalsIgnoreCase(taskDefKey) || "applyEdit".equals(taskDefKey)){
			ZlmtWorksheetNote note = new ZlmtWorksheetNote();
			note.setWorksheetId(zlmtWorksheet.getId());
			note.setNoteType("NOTE_DO");
			note.setNoteContent(zlmtWorksheet.getAct().getComment());
			zlmtWorksheetNoteService.save(note);
		} else if ("applyfeedback".equals(taskDefKey)) {
			Date date = new Date();
			zlmtWorksheet.setRealThruTime(date);
			dao.update(zlmtWorksheet);
			if (procDefId.contains(ActUtils.PD_HANDING_EXCEPTION[0])) { //添加维修记录
				ZlmtMaintenanceRecord record = new ZlmtMaintenanceRecord();
				record.setHostid(zlmtWorksheet.getZlmtResourceWorksheet().getHostid());
				record.setHostname(zlmtWorksheet.getZlmtResourceWorksheet().getHostName());
				record.setAlias(zlmtWorksheet.getZlmtResourceWorksheet().getAlias());
				record.setUser(UserUtils.getUser());
				record.setMaintenanceReason(zlmtWorksheet.getWorksheetReason());
				record.setMaintenanceOperation(zlmtWorksheet.getWorksheetOperation());
				record.setMaintenanceResult(zlmtWorksheet.getWorksheetResult());
				record.setRemark(zlmtWorksheet.getRemarks());
				record.setFromTime(zlmtWorksheet.getRealBeginTime());
				record.setThruTime(date);
				zlmtMaintenanceRecordService.save(record);
			}

		}
		// 提交流程任务
		vars.put("pass", "yes".equals(zlmtWorksheet.getAct().getFlag())? "1" : "0");
		actTaskService.complete(zlmtWorksheet.getAct().getTaskId(), zlmtWorksheet.getAct().getProcInsId(), zlmtWorksheet.getAct().getComment(), vars);
	}
	@Transactional(readOnly = false)
	public void delete(ZlmtWorksheet zlmtWorksheet) {
		super.delete(zlmtWorksheet);
	}
	
}